Ext.define('cfWeb.controller.util.HomeTabController', {
    extend: 'Ext.app.Controller',
    views: ['util.HomeTabView',
        'cfWeb.cfWebComponent.CollapsibleBlock',
        'cfWeb.cfWebComponent.RingChart'
    ],
    stores: ['util.dashboard.RouteUsedStore',
        'util.dashboard.ServiceUsedStore',
        'util.dashboard.AppStatusStore',
        'util.dashboard.MemoryUsedStore',
        'util.AlertStore',
        'util.ServiceAlertStore'
    ],
    init: function() {
        //		setInterval(function() {
        //			// connect to cf every 5 mins
        //			Ext.Ajax.request({
        //				url : 'rest/keepConnect/keeping',
        //				failure : function() {
        //					Ext.msg.alert(cfWebLang.Util.Tip,cfWebLang.Util.RestartBrowse);//  not English
        //				}
        //			});
        //		},85000);
        this.control({
            'homeTabView': {
                afterrender: this.homeTabRendered
            },
            'combo#dashboard-combo': {
                beforerender: this.comboBeforerender,
                select: this.comboSelect
            }

        });
    },

    refs: [{
        ref: 'tabs',
        selector: 'tabView'
    }],
    load: function(me) {
        me.store.load();
    },
    comboBeforerender: function(me) {
        var store = me.getStore();
        store.on('load', function() {
            store.insert(0, {
                name: '全部'
            });
        });
    },
    comboSelect: function(combo, record, eOpts) {
        var data = record[0].data;
        var index = record[0].index;
        dashData = false;
        updateDash(index == undefined ? 'all' : data.name);
        this.homeTabRendered(combo.up('homeTabView'));
    },
    homeTabRendered: function(me, eOpts) {
        var store_routeUsed = me.down('ringChart[itemId=routeUsed]').store;
        var store_serviceUsed = me.down('ringChart[itemId=serviceUsed]').store;
        var store_appStatus = me.down('ringChart[itemId=appStatus]').store;
        var store_memoryUsed = me.down('ringChart[itemId=memoryUsed]').store;
        var loadData = function() {
            var total;
            var count;
            if (dashData == false) {
                setTimeout(function() {
                    loadData()
                }, 500);
                return;
            }
            var data = dashData;
            store_routeUsed.clearData();
            store_routeUsed.add({
                itemId: '1',
                itemName: cfWebLang.HomeTabView.used,
                itemCount: data.routesNum,
                total: data.routesAll
            });
            store_serviceUsed.clearData();
            store_serviceUsed.add({
                itemId: '1',
                itemName: cfWebLang.HomeTabView.used,
                itemCount: data.serviceNum,
                total: data.serviceAll
            });
            store_appStatus.clearData();
            store_appStatus.add({
                itemId: '1',
                itemName: cfWebLang.HomeTabView.run,
                itemCount: data.started,
                total: data.stopped + data.started + data.updating
            },
                {
                    itemId: '2',
                    itemName: cfWebLang.HomeTabView.stop,
                    itemCount: data.stopped,
                    total: data.stopped + data.started + data.updating
                },
                {
                    itemId: '3',
                    itemName: cfWebLang.HomeTabView.update,
                    itemCount: data.updating,
                    total: data.stopped + data.started + data.updating
                });

            if (data.memoryAll < 1024) {
                total = data.memoryAll;
                me.down('ringChart[itemId=memoryUsed]').unit = 'M';
            } else {
                total = data.memoryAll / 1024;
                me.down('ringChart[itemId=memoryUsed]').unit = 'G';
            }

            if (data.memoryUsed < 1024) {
                count = data.memoryUsed;
            } else {
                count = data.memoryUsed / 1024;
            }
            store_memoryUsed.clearData();
            store_memoryUsed.add({
                itemId: '1',
                itemName: cfWebLang.HomeTabView.used,
                itemCount: count.toFixed(2),
                total: total
            });
        }
        loadData();
    }
});
